package com.sdry.web.controller.hpf;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sdry.model.hpf.Page;
import com.sdry.model.hpf.Trolley;
import com.sdry.service.hpf.TrolleyService;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/** 
* 台车控制层
* @author lian 
* @version 1.0
* @time 2018年11月1日 下午3:20:41
*/

@Controller
@RequestMapping(value="/trolleyMana")
public class TrolleyController {

	@Resource
	private TrolleyService trolleyService;  //注入台车service
	
	/**
	 * 分页查询台车信息
	 * @param page 分页条件
	 * @param trolley 查询条件
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/listPageTrolley")
	public void listPageTrolley(Page page, Trolley trolley, HttpServletResponse response) throws Exception{
		Map<String, Object> map = new HashMap<>();
		map.put("limit", page.getLimit());
		map.put("page", page.getPage());
		if (trolley.getTroname() != null ) {
			map.put("troname", trolley.getTroname());
		} else {
			map.put("troname", "");
			trolley.setTroname("");
		}
		int count = trolleyService.countTrolley(trolley);
		List<Trolley> BomList = trolleyService.listPageTrolley(map);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(BomList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 保存台车信息
	 * @param trolley 台车信息实体类
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/saveTrolley")
	public void saveTrolley(Trolley trolley, HttpServletResponse response) throws Exception{
		int isSuccess = 0;
		if (trolley.getTid() == null || "".equals(trolley.getTid())) {
			isSuccess = trolleyService.saveTrolley(trolley);
		} else {
			isSuccess = trolleyService.updateTrolley(trolley);
		}
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 删除台车
	 * @param str tid组成的字符串
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deleteTrolley")
	public void deleteTrolley(@RequestParam("str") String str, HttpServletResponse response) throws Exception{
		int isSuccess = trolleyService.deleteTrolley(str);
		ResponseUtil.write(response, isSuccess);
	}
	
}
